package com.fake_Interviews.controller;

import com.fake_Interviews.Mapper.FeedbackMapper;
import com.fake_Interviews.Mapper.InterviewsMapper;
import com.fake_Interviews.module.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.sql.Blob;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class InterviewerFeedback_Controller {
    public static final String RECORD_DIR = "./record/";
    private final FeedbackMapper feedbackMapper;
    private final InterviewsMapper interviewsMapper;
    public Integer id;

    public InterviewerFeedback_Controller(FeedbackMapper feedbackMapper, InterviewsMapper interviewsMapper) {
        this.feedbackMapper = feedbackMapper;
        this.interviewsMapper = interviewsMapper;
    }

    @PostMapping("/interviewerFeedback")
    public ResponseEntity<String> interviewerFeedback(@RequestBody Map<String,String> data) {
        Feedback feedback = new Feedback();
        // 拿到数据库Feedback里所有的数据
        FeedbackExample example = new FeedbackExample();
        List<Feedback> list = feedbackMapper.selectByExample(example);

        // 第几场面试
        id = Integer.parseInt(data.get("id"));

        // 根据第几场面试的id来判断是不是存在，如果存在，就直接修稿对应ID的反馈字段，否则创建新的面试场次
        for (int i = 0; i < list.size(); i++) {
            if(id == list.get(i).getInterviewsId()) {
                feedback.setInterviewtubefeedback(data.get("interviewerFeedback"));
                example.createCriteria().andIdEqualTo(id);
                feedbackMapper.updateByExampleSelective(feedback,example);
                return ResponseEntity.ok().header("Custom-Header","ok").body("提交成功！");
            }
        }

        // 如果在数据库找不到是第几场的面试，就创建新场次的面试
        feedback.setId(id);
        feedback.setInterviewsId(id);
        feedback.setInterviewtubefeedback(data.get("interviewerFeedback"));
        feedbackMapper.insert(feedback);
        return ResponseEntity.ok().header("Custom-Header","ok").body("提交成功！");
    }


    // 上传录音
    // @RequestParam MultipartFile file
    // HttpServletResponse response
    // path : C:\Users\BRET\Desktop\录音
    // /root/record/" + id + ".wav"
    @PostMapping("/uploadRecord")
    public ResponseEntity<String> uploadRecord(@RequestParam MultipartFile file) throws IOException {
        byte[] test = file.getBytes();
        String path = RECORD_DIR + id + ".wav";
        File pathFile = new File(path);
        if (!pathFile.exists()) {
            pathFile.createNewFile();
        }
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(pathFile));
        outputStream.write(test);
        outputStream.flush();
        // 向反馈表中保存录音路径
        Feedback feedback = new Feedback();
        feedback.setId(id);
        feedback.setRecordingfiles(path);
        feedbackMapper.updateByPrimaryKeySelective(feedback);
        id = null;

        return null;
    }
}
